from django.db import models
from app.account.models import UserInfo
from django.utils.timezone import now


# Create your models here.

class ActivityType(models.Model):
    """活动类型名"""
    name = models.CharField(verbose_name='类型名', max_length=20, default='', blank=False)

    description = models.CharField(verbose_name='类型描述', max_length=200, default='', blank=True)

    from app.aoduo_admin.models import AdminAccount
    creator = models.ForeignKey(AdminAccount, verbose_name='创建人', default='', blank=False, on_delete=models.CASCADE)

    create_time = models.DateTimeField(verbose_name='创建时间', default=now, blank=False)

    class Meta:
        verbose_name = '活动类型名'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'outdoor_ActivityType'
        ordering = ['-create_time']

    def __str__(self):
        return self.name


class ActivityMission(models.Model):
    """活动任务表"""
    type_choice = {
        ('1', '地点完成'),
        ('2', '无地点完成')
    }

    name = models.CharField(verbose_name='任务名', max_length=100, blank=False, default='')

    mission_type = models.CharField(verbose_name='任务类型', max_length=20, choices=type_choice, blank=False, default='')

    order = models.IntegerField(verbose_name='顺序编号', default=1, blank=True)

    end_time = models.TimeField(verbose_name='截止时间点', null=True, blank=True)

    latitude = models.FloatField(verbose_name='纬度', null=True, blank=True)

    longitude = models.FloatField(verbose_name='经度', null=True, blank=True)

    finish = models.BooleanField(verbose_name='是否完成', default=False, blank=False)

    class Meta:
        verbose_name = '活动任务'
        verbose_name_plural = verbose_name + '列表'
        ordering = ['-end_time', 'order']
        db_table = 'outdoor_ActivityMission'

    def __str__(self):
        return self.name


class Activity(models.Model):
    """活动表"""
    status_choice = {
        ('1', '未发布'),
        ('2', '已发布'),
        ('3', '开始报名'),
        ('4', '截止报名'),
        ('5', '进行中'),
        ('6', '已结束')
    }

    clock_in_type_choice = {
        ('1', '时间线任务打卡'),
        ('2', '非时间线任务式打卡')
    }

    """基础信息"""
    name = models.CharField(verbose_name='活动名', max_length=25, blank=False, default='')

    activity_intro = models.TextField(verbose_name='活动介绍', blank=False, default='')

    maximum_number_of_people = models.IntegerField(verbose_name='人数上限', blank=False, default=1)

    status = models.CharField(verbose_name='活动状态', choices=status_choice, max_length=20, default='1', blank=False)

    activity_type = models.ForeignKey(ActivityType, verbose_name='活动类型', blank=False, default='',
                                      on_delete=models.CASCADE)

    clock_in_type = models.CharField(verbose_name='打卡方式', choices=clock_in_type_choice, max_length=20, blank=False,
                                     default='1')

    """时间信息"""
    create_time = models.DateTimeField(verbose_name='创建时间', default=now, blank=False)

    last_mod_time = models.DateTimeField(verbose_name='最后修改时间', default=now, blank=False)

    release_time = models.DateTimeField(verbose_name='发布时间', blank=True, null=True)

    sign_up_start_time = models.DateTimeField(verbose_name='报名开始时间', blank=True, null=True)

    sign_up_end_time = models.DateTimeField(verbose_name='报名截止时间', blank=True, null=True)

    begin_time = models.DateTimeField(verbose_name='活动开始时间', blank=True, null=True)

    end_time = models.DateTimeField(verbose_name='活动结束时间', blank=True, null=True)

    """人员相关"""
    sponsor = models.ForeignKey(UserInfo, verbose_name='发起人', default='', blank=False, on_delete=models.CASCADE,
                                related_name='sponsor')

    leader = models.ManyToManyField(UserInfo, verbose_name='领队人', default='', blank=True, related_name='leader')

    participants = models.ManyToManyField(UserInfo, verbose_name='参加人', default='', blank=True)

    group_name = models.CharField(verbose_name='组织名', default='', blank=True, max_length=20)

    """自定义规则"""

    is_charge = models.BooleanField(verbose_name='是否收费', default=False)

    is_password = models.BooleanField(verbose_name='是否密码', default=False)

    is_realtime_locate = models.BooleanField(verbose_name='是否实时定位', default=False)

    is_sign_in = models.BooleanField(verbose_name='是否打卡签到', default=False)

    """活动内容"""
    mission = models.ManyToManyField(ActivityMission, verbose_name='任务点', blank=True)

    """其他内容"""
    views = models.BigIntegerField(verbose_name='浏览量', default=0, blank=False)

    star_number = models.BigIntegerField(verbose_name='收藏量', default=0, blank=False)

    class Meta:
        verbose_name = '户外活动'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'outdoor_Activity'
        ordering = ['-create_time']

    def __str__(self):
        return self.name


class ActivityTypeTemplate(models.Model):
    """户外活动类型模板"""
    related_activity_type = models.ForeignKey(ActivityType, verbose_name='关联活动类型', default='', blank=False,
                                              on_delete=models.CASCADE)

    is_charge = models.BooleanField(verbose_name='是否收费', default=False)

    is_password = models.BooleanField(verbose_name='是否密码', default=False)

    is_realtime_locate = models.BooleanField(verbose_name='是否实时定位', default=False)

    is_sign_in = models.BooleanField(verbose_name='是否打卡签到', default=False)

    mission = models.ManyToManyField(ActivityMission, verbose_name='任务点', blank=True)

    class Meta:
        verbose_name = '活动类型模板'
        verbose_name_plural = verbose_name + '列表'
        db_table = 'outdoor_ActivityTemplate'

    def __str__(self):
        return str(self.related_activity_type.name)
